package com.senior.completable;

import com.yu.threadpool.ThreadPoolUntils;

import java.util.concurrent.*;

public class CompletableFutureApiDemo {


    public static void main(String[] args) {
        //通过这种方式，可以将结果集聚合合并等等
        Integer join = CompletableFuture.supplyAsync(() -> 10)
                .thenCombine(CompletableFuture.supplyAsync(() -> 20), (r1, r2) -> r1 + r2)
                .thenCombine(CompletableFuture.supplyAsync(() -> 20), ((r1, r2) -> r1 + r2)).join();
        System.out.println(join);
    }


    public static void ma3(String[] args) {
//        CompletableFuture.supplyAsync(() -> {
//            return 1;
//        }).thenApply(f->{
//            return f+1;
//        }).thenAccept(r -> System.out.println(r));
//        //thenAccept 无返回结果，只是需要上面的异步数据  有输入无返回
//        //thenRun 无输入，无返回信息
//        //thenApply 有输入结果，有返回信息
        ThreadPoolExecutor threadPool = ThreadPoolUntils.getThreadPool();
        Integer join = CompletableFuture.supplyAsync(() -> { //可以把这段代码想象成两个人比赛玩消消乐，谁先完成谁就赢了，哪类联网竞技型的
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 1;
        }, threadPool).applyToEither(CompletableFuture.supplyAsync(() -> {// 这就是两个异步线程并发执行
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 2;
        }), r -> {  //然后将先执行完的返回值结果返回出去
            return r;
        }).join();
        System.out.println(join);

        threadPool.shutdown();
    }








    public static void m2(String[] args) {
        ThreadPoolExecutor threadPool = ThreadPoolUntils.getThreadPool();

        Integer join = CompletableFuture.supplyAsync(() -> {
            return 1;
        }, threadPool).handle((f,e) -> { //thenApply  和 thenApplyAsync区别就是，前面的让原本的线程池执行，后面的会新搞一条线程
            int i = 10/0;
            return f + 2;

        }).thenApply(f -> {
            return f + 3;
        }).whenComplete((v, e) -> {
            if (e == null) {
                System.out.println("result：" + v);
            }
        }).exceptionally(e -> {
            e.printStackTrace();
            return null;
        }).join();
        System.out.println(join);
        threadPool.shutdown();
    }




    public static void m1(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        ThreadPoolExecutor threadPool = ThreadPoolUntils.getThreadPool();
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 1;
        },threadPool);
        //get()  立刻阻塞
        Integer result = future.get();
        //只阻塞两秒
        //future.get(2,TimeUnit.SECONDS);
        //获取现在的值，如果获取不到，就把1111给过去
        //future.getNow(1111);
        //complete()  打断，打断成功返回ture，然后把2给到结果，打断失败返回false，结果就是get()的值
        //System.out.println(future.complete(2) + ":" + future.get());
        threadPool.shutdown();
    }
}
